package com.naizhuai.ui.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.Button;

import com.google.gson.Gson;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.TaskActionApiStore;
import com.naizhuai.ui.components.swipeback.SwipeBackActivity;
import com.naizhuai.ui.entity.Bus.Bus;
import com.naizhuai.ui.entity.Task.TaskChartData;
import com.naizhuai.ui.task.adapter.DataUpdateAdapter;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.RxBus;

import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DataUpdateActivity extends SwipeBackActivity {
    public static final String TASK_ID = "task_id";
    public static void start(Context context, String taskid){
        Intent intent = new Intent(context, DataUpdateActivity.class);
        intent.putExtra(DataUpdateActivity.TASK_ID, taskid);
        context.startActivity(intent);
    }
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.submit)
    Button exitLogin;
    private DataUpdateAdapter mDataAdapter;
    private TaskActionApiStore mTaskActionApiStore;
    private String mTaskid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        mTaskid = getStrExtra(TASK_ID);
        if (mTaskActionApiStore == null) {
            mTaskActionApiStore = TaskActionApiIml.getInstance();
        }
        mTaskActionApiStore.task_data_page(mTaskid)
                           .subscribe(new NetWorkObserver<List<TaskChartData>>() {
                               @Override
                               protected void onFail(String errormsg) {
                               }
                               @Override
                               protected void onSuccess(List<TaskChartData> result) {
                                   mDataAdapter.setResult(result);
                               }
                           });
    }

    private void initView() {
        toolbar.setNavigationOnClickListener(v -> finish());
        recycleView.setNestedScrollingEnabled(false);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mDataAdapter = new DataUpdateAdapter());
    }
   
    @Override
    public int getLayoutId() {
        return R.layout.activity_data_update;
    }

    @OnClick(R.id.submit)
    public void onViewClicked() {
        List<TaskChartData> mlist=mDataAdapter.getChartData();
        showWaitDiaLog();
        mTaskActionApiStore.add_task_data_page(mTaskid,new Gson().toJson(mlist))
                           .subscribe(new NetWorkObserver<String>() {
                               @Override
                               protected void onFail(String errormsg) {
                                   hideWaitDialog();
                                   shortToast(getString(R.string.action_fail));
                               }

                               @Override
                               protected void onSuccess(String result) {
                                   hideWaitDialog();
                                   if (ConstantsState.isResultOk(result)){
                                       shortToast(getString(R.string.action_ok));
                                       finish();
                                       RxBus.getDefault().postSticky(Bus.ReFreshTask.class);
                                   }else {
                                       shortToast(getString(R.string.action_fail));
                                   }
                               }
                           });
    }
}
